/*
 * StartApplication.java
 *
 * Created on Jul 23, 2010 12:17:12 AM;
 */

package org.sunspotworld;

import com.sun.spot.core.resources.transducers.ISwitch;
import com.sun.spot.core.resources.transducers.ITriColorLED;
import com.sun.spot.core.resources.transducers.LEDColor;
import com.sun.spot.core.util.Utils;
import com.sun.spot.edemo.EDemoBoard;
import com.sun.spot.espot.service.BootloaderListenerService;
import com.sun.spot.multihop.io.j2me.radiogram.Radiogram;
import com.sun.spot.multihop.io.j2me.radiogram.RadiogramConnection;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * The startApp method of this class is called by the VM to start the
 * application.
 * 
 * The manifest specifies this class as MIDlet-1, which means it will
 * be selected for execution.
 */
public class StartApplication extends MIDlet {

    private ITriColorLED [] leds = EDemoBoard.getInstance().getLEDs();
    private ISwitch [] sw = EDemoBoard.getInstance().getSwitches();
    private String port = "51";
    private boolean running = true;
    private boolean pressed = false;
    private int sayingsCounter = 0;

    /**
     * The startApp will be used to select a number to send to the http connected
     * sun spot. This number will determine what message is sent to twitter. The
     * message is selected by pressing switch 1. The selection number is notified
     * by the leds. The 8th led is red because it will end the program when sent.
     * Switch 2 controls the radio communication. It will send the integer of the
     * message you have selected to the http communication spot.
     * @throws MIDletStateChangeException
     */
    protected void startApp() throws MIDletStateChangeException {
        new BootloaderListenerService().start();   // monitor the USB (if connected) and recognize commands from host

        RadiogramConnection rdgConnection = null;
        Radiogram rdg = null;

        //Establish radio communication with http connected spot.
        try {
                rdgConnection = (RadiogramConnection) Connector.open("radiogram://broadcast:" + port);
                rdg = (Radiogram)rdgConnection.newDatagram(rdgConnection.getMaximumLength());
        } catch (IOException ex) {
                ex.printStackTrace();
        }

        //Constantly run unless you select option 7
        while(running) {

            //Pressing switch 1 (left switch) will change the option number
            if((sw[0].isClosed() && sw[1].isOpen()) && !pressed) {
                sayingsCounter++;
                if(sayingsCounter == 8)
                    sayingsCounter = 0;
                pressed = true;
            }
            //Pressing switch 2(right switch) will send the option number to other spot
            else if((sw[0].isOpen() && sw[1].isClosed()) && !pressed) {
                if(sayingsCounter == 7) {
                    running = false;
                }
                else {
                    try {
                        rdg.reset();
                        rdg.writeInt(sayingsCounter);
                        rdgConnection.send(rdg);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    for(int i = 0; i < leds.length; i++) {
                        leds[i].setColor(LEDColor.GREEN);
                        leds[i].setOn();
                        Utils.sleep(200);
                        leds[i].setOff();
                    }
                }
                pressed = true;
            }
            //This will unlock the switches after one of them is pressed
            else if(sw[0].isOpen() && sw[1].isOpen()) {
                pressed = false;
            }

            if(sayingsCounter == 7) {
                leds[sayingsCounter].setColor(LEDColor.RED);
                leds[sayingsCounter].setOn();
            }
            else {
                leds[sayingsCounter].setColor(LEDColor.GREEN);
                leds[sayingsCounter].setOn();
            }

            leds[sayingsCounter].setOff();
            

        }

        notifyDestroyed();                      // cause the MIDlet to exit
    }

    protected void pauseApp() {
        // This is not currently called by the Squawk VM
    }

    /**
     * Called if the MIDlet is terminated by the system.
     * I.e. if startApp throws any exception other than MIDletStateChangeException,
     * if the isolate running the MIDlet is killed with Isolate.exit(), or
     * if VM.stopVM() is called.
     * 
     * It is not called if MIDlet.notifyDestroyed() was called.
     *
     * @param unconditional If true when this method is called, the MIDlet must
     *    cleanup and release all resources. If false the MIDlet may throw
     *    MIDletStateChangeException  to indicate it does not want to be destroyed
     *    at this time.
     */
    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
        for (int i = 0; i < 8; i++) {
            leds[i].setOff();
        }
    }
}
